package com.zh.tea.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zh.tea.dao.FeedbackMapper;
import com.zh.tea.entity.Feedback;
import com.zh.tea.entity.FeedbackExample;
import com.zh.tea.service.FeedbackService;
import com.zh.tea.utils.SessionFactoryUtils;

public class FeedbackServiceImpl implements FeedbackService {

	@Override
	public int add(Feedback feedback) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		FeedbackMapper fm = session.getMapper(FeedbackMapper.class);
		//用户提交反馈
		int a = fm.insert(feedback);
		session.commit();
		session.close();
		return a;
	}

	@Override
	public List<Feedback> selAll() {
		// TODO Auto-generated method stub
		//查询所有反馈
		SqlSession session = SessionFactoryUtils.getSqlSessionFactory().openSession();
		FeedbackMapper fm = session.getMapper(FeedbackMapper.class);
		List<Feedback> list = fm.selectByExample(null);				
		return list;
	}
	
	@Override
	public PageInfo<Feedback> getFeedback(Feedback feedback,Integer pagNo,Integer pageSize) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		FeedbackMapper feedbackMapper=session.getMapper(FeedbackMapper.class);
		FeedbackExample feedbackexm=new FeedbackExample();
		FeedbackExample.Criteria criteria=feedbackexm.createCriteria();				
					
		PageHelper.startPage(pagNo, pageSize);
		List<Feedback> listt=feedbackMapper.selectByExample(feedbackexm);
		System.out.println(listt.isEmpty());
		PageInfo<Feedback> pagefeedback=new PageInfo<>(listt); 
		session.close();
		return pagefeedback;
	}
	
	@Override
	public Feedback sel(Integer id) {
		// TODO Auto-generated method stub
		SqlSession session = SessionFactoryUtils.getSqlSessionFactory().openSession();
		FeedbackMapper fm = session.getMapper(FeedbackMapper.class);
		FeedbackExample fe = new FeedbackExample();
		//按id查找
		//FeedbackExample.Criteria CriteriaId = fe.createCriteria();
		Feedback fb = fm.selectByPrimaryKey(id);
		if(fb!=null) {
			return fb;
		}
		return null;
	}
	
	@Override
	public int del(Integer id) {
		// TODO Auto-generated method stub
		SqlSession session=SessionFactoryUtils.getSqlSessionFactory().openSession();
		FeedbackMapper fm = session.getMapper(FeedbackMapper.class);
		//管理员不想看你的反馈并删除了它
		int a = fm.deleteByPrimaryKey(id);
		session.commit();
		session.close();
		return a;
	}

	

}
